<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>mysql主从数据库不同步的2种解决方法 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>mysql主从数据库不同步的2种解决方法</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/MySQL/">MySQL</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;发现Mysql的主从数据库没有同步 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;先上Master库： </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt;show processlist;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看下进程是否Sleep太多。发现很正常。 </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">show master status;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;也正常。 </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">mysql&gt; show master status; </div><div class="line">+-------------------+----------+--------------+-------------------------------+ </div><div class="line">| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | </div><div class="line">+-------------------+----------+--------------+-------------------------------+ </div><div class="line">| mysqld-bin.000001 | 3260 | | mysql,<span class="built_in">test</span>,information_schema | </div><div class="line">+-------------------+----------+--------------+-------------------------------+ </div><div class="line">1 row <span class="keyword">in</span> <span class="built_in">set</span> (0.00 sec)</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再到Slave上查看 </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">mysql&gt; show slave status\G;</div><div class="line">Slave_IO_Running: Yes </div><div class="line">Slave_SQL_Running: No</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可见是Slave不同步 </p>
<h2 id="下面介绍两种解决方法："><a href="#下面介绍两种解决方法：" class="headerlink" title="下面介绍两种解决方法："></a>下面介绍两种解决方法：</h2><h3 id="方法一：忽略错误后，继续同步"><a href="#方法一：忽略错误后，继续同步" class="headerlink" title="方法一：忽略错误后，继续同步"></a>方法一：忽略错误后，继续同步</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;该方法适用于主从库数据相差不大，或者要求数据可以不完全统一的情况，数据要求不严格的情况 </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">stop slave; </div><div class="line"><span class="comment">#表示跳过一步错误，后面的数字可变 </span></div><div class="line"><span class="built_in">set</span> global sql_slave_skip_counter =1; </div><div class="line">start slave;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;之后再用</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; show slave status\G;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看： </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">Slave_IO_Running: Yes </div><div class="line">Slave_SQL_Running: Yes</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ok，现在主从同步状态正常了。。。 </p>
<h3 id="方式二：重新做主从，完全同步"><a href="#方式二：重新做主从，完全同步" class="headerlink" title="方式二：重新做主从，完全同步"></a>方式二：重新做主从，完全同步</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;该方法适用于主从库数据相差较大，或者要求数据完全统一的情况 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解决步骤如下： </p>
<ol>
<li>先进入主库，进行锁表，防止数据写入 </li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用命令： </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; flush tables with <span class="built_in">read</span> lock;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意：该处是锁定为只读状态，语句不区分大小写 </p>
<ol>
<li>进行数据备份 </li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;把数据备份到mysql.bak.sql文件 </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@server01 mysql]<span class="comment">#mysqldump -uroot -p -hlocalhost &gt; mysql.bak.sql</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里注意一点：数据库备份一定要定期进行，可以用shell脚本或者python脚本，都比较方便，确保数据万无一失 </p>
<ol>
<li>查看master 状态 </li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">mysql&gt; show master status; </div><div class="line">+-------------------+----------+--------------+-------------------------------+ </div><div class="line">| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | </div><div class="line">+-------------------+----------+--------------+-------------------------------+ </div><div class="line">| mysqld-bin.000001 | 3260 | | mysql,<span class="built_in">test</span>,information_schema | </div><div class="line">+-------------------+----------+--------------+-------------------------------+ </div><div class="line">1 row <span class="keyword">in</span> <span class="built_in">set</span> (0.00 sec)</div></pre></td></tr></table></figure>
<ol>
<li>把mysql备份文件传到从库机器，进行数据恢复 </li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用scp命令 </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@server01 mysql]<span class="comment"># scp mysql.bak.sql root@192.168.128.101:/tmp/</span></div></pre></td></tr></table></figure>
<ol>
<li>停止从库的状态 </li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; stop slave;</div></pre></td></tr></table></figure>
<ol>
<li>然后到从库执行mysql命令，导入数据备份 </li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; <span class="built_in">source</span> /tmp/mysql.bak.sql</div></pre></td></tr></table></figure>
<ol>
<li>设置从库同步，注意该处的同步点，就是主库show master status信息里的| File| Position两项 </li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">change master to master_host = <span class="string">'192.168.128.100'</span>, master_user = <span class="string">'rsync'</span>, master_port=3306, master_password=<span class="string">''</span>, master_log_file = <span class="string">'mysqld-bin.000001'</span>, master_log_pos=3260;</div></pre></td></tr></table></figure>
<ol>
<li>重新开启从同步 </li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt; stop slave;</div></pre></td></tr></table></figure>
<ol>
<li>查看同步状态 </li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">mysql&gt; show slave status\G; </div><div class="line">Slave_IO_Running: Yes </div><div class="line">Slave_SQL_Running: Yes</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;好了，同步完成啦。</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/MySQL/33. mysql主从数据库不同步的2种解决方法/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
